package javabase.xml.jsoup;

import cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException;
import cn.wanghaomiao.xpath.model.JXDocument;
import cn.wanghaomiao.xpath.model.JXNode;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Xpath查询
 */
public class JsoupDemo6 {
    public static void main(String[] args) throws IOException, XpathSyntaxErrorException {
        String path = JsoupDemo6.class.getClassLoader().getResource("student.xml").getPath();
        Document document = Jsoup.parse(new File(path), "utf-8");
        //1.根据Document对象创建JXDocument对象（JXDocument对象支持Xpath语法）
        JXDocument jxDocument = new JXDocument(document);
        //2.结合Xpath语法
        // 2.1查询所有student标签
        List<JXNode> jxNodes = jxDocument.selN("//student");
        for (JXNode jxNode : jxNodes) {
            System.out.println(jxNode);
        }
        System.out.println("************************************");
        //2.2查询所有student标签下的name标签
        List<JXNode> jxNodes2 = jxDocument.selN("//student/name");
        for (JXNode jxNode : jxNodes2) {
            System.out.println(jxNode);
        }
        System.out.println("************************************");
        //2.3查询所有student标签下的带有xpath属性的name标签
        List<JXNode> jxNodes3 = jxDocument.selN("//student/name[@xpath]");
        for (JXNode jxNode : jxNodes3) {
            System.out.println(jxNode);
        }
        System.out.println("************************************");
        //2.4查询所有student标签下的带有xpath属性的且xpath属性值为haha的name标签
        List<JXNode> jxNodes4 = jxDocument.selN("//student/name[@xpath=haha]");
        for (JXNode jxNode : jxNodes4) {
            System.out.println(jxNode);
        }
        System.out.println("************************************");
    }

}
